<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>新的特性</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="migration55.incompatible.html">不向后兼容的变更</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="migration55.deprecated.html">PHP 5.5.x 中废弃的特性</a></div>
 <div class="up"><a href="migration55.html">从 PHP 5.4.x 迁移到 PHP 5.5.x</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="migration55.new-features" class="sect1">
  <h2 class="title">新的特性</h2>

  <div class="sect2" id="migration55.new-features.generators">
   <h3 class="title">新增 <a href="language.generators.html" class="link">Generators</a></h3>

   <p class="para">
    Support for <a href="language.generators.html" class="link">generators</a> has been
    added via the <strong class="command">yield</strong> keyword. Generators provide an easy
    way to implement simple iterators without the overhead or complexity of
    implementing a class that implements the <strong class="classname">Iterator</strong>
    interface.
   </p>

   <p class="para">
    A simple example that reimplements the <span class="function"><a href="function.range.html" class="function">range()</a></span>
    function as a generator (at least for positive <em>step</em>
    values):
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">xrange</span><span style="color: #007700">(</span><span style="color: #0000BB">$start</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$limit</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$step&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$start</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">$limit</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">$step</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">yield&nbsp;$i</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">'Single&nbsp;digit&nbsp;odd&nbsp;numbers:&nbsp;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;注意保存在内存中的数组绝不会被创建或返回&nbsp;*/&nbsp;<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">xrange</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">9</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$number</span><span style="color: #DD0000">&nbsp;"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <p class="para">以上例程会输出：</p>
    <div class="example-contents screen">
<div class="cdata"><pre>
Single digit odd numbers: 1 3 5 7 9 
</pre></div>
    </div>
   </div>
  </div>

  <div class="sect2" id="migration55.new-features.finally">
   <h3 class="title">新增 <a href="language.exceptions.html" class="link"><em>finally</em></a> 关键字</h3>

   <p class="para">
    <em>try</em>-<em>catch</em> blocks now support a
    <a href="language.exceptions.html" class="link"><em>finally</em></a>
    block for code that should be run regardless of whether an exception has
    been thrown or not.
   </p>
  </div>

  <div class="sect2" id="migration55.new-features.foreach-list">
   <h3 class="title"><a href="control-structures.foreach.html" class="link"><em>foreach</em></a> 现在支持 <span class="function"><a href="function.list.html" class="function">list()</a></span></h3>

   <p class="para">
   <a href="control-structures.foreach.html" class="link">foreach</a> 控制结构现在支持通过 <span class="function"><a href="function.list.html" class="function">list()</a></span> 构造将嵌套数组分离到单独的变量。例如：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">],<br />];<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">as&nbsp;list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"A:&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">;&nbsp;B:&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <p class="para">以上例程会输出：</p>
    <div class="example-contents screen">
<div class="cdata"><pre>
A: 1; B: 2
A: 3; B: 4
</pre></div>
    </div>
   </div>

   <p class="para">
    关于 <a href="control-structures.foreach.html#control-structures.foreach.list" class="link">foreach</a> 更深入的文档可参考相关手册页面。
   </p>
  </div>

  <div class="sect2" id="migration55.new-features.empty">
   <h3 class="title"><span class="function"><a href="function.empty.html" class="function">empty()</a></span> 支持任意表达式</h3>

   <p class="para">
    <span class="function"><a href="function.empty.html" class="function">empty()</a></span> 现在支持传入一个任意表达式，而不仅是一个变量。例如：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">always_false</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(empty(</span><span style="color: #0000BB">always_false</span><span style="color: #007700">()))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'This&nbsp;will&nbsp;be&nbsp;printed.'</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(empty(</span><span style="color: #0000BB">true</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'This&nbsp;will&nbsp;not&nbsp;be&nbsp;printed.'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <p class="para">以上例程会输出：</p>
    <div class="example-contents screen">
<div class="cdata"><pre>
This will be printed.
</pre></div>
    </div>
   </div>
  </div>

  <div class="sect2" id="migration55.new-features.const-dereferencing">
   <h3 class="title"><span class="type"><a href="language.types.array.html" class="type array">array</a></span> and <span class="type"><a href="language.types.string.html" class="type string">string</a></span> literal dereferencing</h3>

   <p class="para">
    <span class="type"><a href="language.types.array.html" class="type Array">Array</a></span> and <span class="type"><a href="language.types.string.html" class="type string">string</a></span> literals can now be
    dereferenced directly to access individual elements and characters:

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Array&nbsp;dereferencing:&nbsp;'</span><span style="color: #007700">;<br />echo&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">'String&nbsp;dereferencing:&nbsp;'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <p class="para">以上例程会输出：</p>
     <div class="example-contents screen">
<div class="cdata"><pre>
Array dereferencing: 1
String dereferencing: P
</pre></div>
     </div>
    </div>
   </p>
  </div>

  <div class="sect2" id="migration55.new-features.password">
   <h3 class="title">新的密码哈希 API</h3>

   <p class="para">
    A <a href="book.password.html" class="link">新的密码哈希 API</a> that makes
    it easier to securely hash and manage passwords using the same underlying
    library as <span class="function"><a href="function.crypt.html" class="function">crypt()</a></span> in PHP has been added. See the
    documentation for <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span> for more detail.
   </p>
  </div>

  <div class="sect2" id="migration55.new-features.windows-apache">
   <h3 class="title">Apache 2.4 handler supported on Windows</h3>

   <p class="para">
    The Apache 2.4 handler SAPI is now supported on Windows.
   </p>
  </div>

  <div class="sect2" id="migration55.new-features.gd">
   <h3 class="title">改进 GD</h3>

   <p class="para">
    对 GD 扩展做了多方面的改进，包括：
   </p>

   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      翻转支持使用新的 <span class="function"><a href="function.imageflip.html" class="function">imageflip()</a></span> 函数。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      高级裁剪支持使用 <span class="function"><a href="function.imagecrop.html" class="function">imagecrop()</a></span> &amp;  <span class="function"><a href="function.imagecropauto.html" class="function">imagecropauto()</a></span> 函数。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      WebP 的读写分别支持使用 <span class="function"><a href="function.imagecreatefromwebp.html" class="function">imagecreatefromwebp()</a></span> &amp; <span class="function"><a href="function.imagewebp.html" class="function">imagewebp()</a></span> 。
     </span>
    </li>
   </ul>
  </div>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="migration55.incompatible.html">不向后兼容的变更</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="migration55.deprecated.html">PHP 5.5.x 中废弃的特性</a></div>
 <div class="up"><a href="migration55.html">从 PHP 5.4.x 迁移到 PHP 5.5.x</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
